
import WeDisplayShaders from './WeDisplayShaders';

const { WeDefineHasNormalMap, WeDefineHasContour, WeDefineHasICON } = WeDisplayShaders;

const WeSurfaceShaders = `

czm_material czm_getMaterial(czm_materialInput materialInput) {
    czm_material material = czm_getDefaultMaterial(materialInput);

    float minValue = Weget_minValue();
    float maxValue = Weget_maxValue();
    float filterMin = Weget_filterMin();
    float filterMax = Weget_filterMax();
    float offset = Weget_offset();
    float scale = Weget_scale();
    bool shouldClamp = Weget_clamp() > 0.0;
    bool shouldAnime = Weget_shouldAnime() > 0.0;
    bool shouldFlipX = Weget_shouldFlipX() > 0.0;
    bool shouldFlipY = Weget_shouldFlipY() > 0.0;
    float sectPositionZ = Weget_sectPositionZ();
    float surfaceAlpha = Weget_surfaceAlpha();
#ifdef IS_SAMPLER3D
    vec3 uv = vec3(materialInput.st,sectPositionZ);
#else
    vec2 uv = materialInput.st;
#endif

    vec4 worldExtent = Weget_extent();
    vec4 localExtent = Weget_localExtent();

    vec2 lonLat = WeGetImageryLonLat(uv.xy,localExtent);
    uv.xy = WeGetImageryLocalUV(lonLat,worldExtent);

    if(shouldFlipY)
    {
        uv.y = 1.0 - uv.y;
    }
    float value = WeDecodeValue(uv);

    float i = (value - minValue) / (maxValue - minValue);
    vec4 color = texture(u_TFTexture,vec2(i,0.0));
    if(shouldClamp){
      color.rgb = WeGetRampColor(color.rgb,u_TFTexture,value,minValue,maxValue);
    }

    color.a = WeIsNodata(value,minValue,filterMin,filterMax) ? 0.0 : color.a;

    #ifdef ${WeDefineHasNormalMap}
        WeNormalEffect(color,uv);
    #endif

    #ifdef ${WeDefineHasContour}
        WeContour(value,color);
    #endif

    #ifdef ${WeDefineHasICON}
        vec4 colorIcon = WeIcon(uv);
        // color = mix(color,colorIcon,colorIcon.a);
        color = colorIcon;
    #endif

    material.diffuse = color.rgb;
    material.alpha = color.a * surfaceAlpha;
    material.specular = 0.0; // Specular mountain tops
    material.shininess = 0.0; // Sharpen highlight
    if(material.alpha == 0.0) discard;
    return material;
}
`;


export default WeSurfaceShaders;
